package com.example.tuter.activity;

import static com.example.tuter.util.ImageUploadHelper.CHOOSE_PHOTO;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.tuter.MyApplication;
import com.example.tuter.R;
import com.example.tuter.activity.util.SendPhotoActivity;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.ImageUploadHelper;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//发布帖子类
public class PostPublishActivity extends BaseActivity implements SendPhotoActivity {



    private String TAG = PostPublishActivity.class.getSimpleName();

    private ImageView picture;

    ImageUploadHelper util;

    private EditText editText_title;
    private EditText editText_description;
    private ImageView imageviewSendPost;
    private ImageView imageViewChoosePhoto;
    private ImageView imageView_back_arrow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //配置图片上传util
        util = new ImageUploadHelper(this,this,imageviewSendPost,imageViewChoosePhoto,imageView_back_arrow);

    }


    @Override
    public void onRequestPermissionsResult(int RequestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(RequestCode, permissions, grantResults);
        switch (RequestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    util.openAlbum();
                } else {
                    Toast.makeText(this, "you denied", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
        switch (RequestCode) {
            case CHOOSE_PHOTO:
                if (ResultCode == RESULT_OK) {
                    util.handleImageOnKitKat(data,picture);
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void sendPhotoContent() {
        String title = editText_title.getText().toString();
        String description = editText_description.getText().toString();
        if (!title.isEmpty()) {//判断是否有输入
            //发送帖子
            CommunityAPI communityAPI = Constant.r.create(CommunityAPI.class);
            Call<Message<Post>> call = communityAPI.publishPost(title, description, Constant.POST_TYPE_NONE, util.getUploadImageUri());
            call.enqueue(new Callback<Message<Post>>() {
                @Override
                public void onResponse(Call<Message<Post>> call, Response<Message<Post>> response) {
                    //发送成功
                    Toast.makeText(MyApplication.getContext(), "发送成功", Toast.LENGTH_SHORT).show();
                    //发送帖子后跳转回主页
                    finish();
                }

                @Override
                public void onFailure(Call<Message<Post>> call, Throwable t) {
                    Toast.makeText(MyApplication.getContext(), "发送失败", Toast.LENGTH_SHORT).show();
                }
            });

        }else{
            //请输入标题 toast
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void bindEvent() {

    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_send_post);
        //获取组件
        //发送图片按钮
        imageviewSendPost = (ImageView) findViewById(R.id.activity_imageview_send_tie);
        imageViewChoosePhoto = (ImageView) findViewById(R.id.activity_button_choose_photo);
        imageView_back_arrow = (ImageView) findViewById(R.id.activity_send_tie_imageView);

        picture = (ImageView) findViewById(R.id.activity_send_tie_photo_view);  //显示的图片预览
        //获取用户输入
        editText_title = (EditText) findViewById(R.id.activity_send_tie_title);
        editText_description = (EditText) findViewById(R.id.activity_send_tie_description);
        //获取结束
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_send_post;
    }
}
